package com.zxj.friend.controller.exam;

import com.zxj.common.core.contains.HttpConstants;
import com.zxj.common.core.controller.BaseController;
import com.zxj.common.core.domain.R;
import com.zxj.common.core.domain.vo.PageVO;
import com.zxj.friend.domain.exam.dto.ExamQueryPageDTO;
import com.zxj.friend.domain.exam.dto.ExamRankDTO;
import com.zxj.friend.domain.exam.vo.ExamRankVO;
import com.zxj.friend.domain.exam.vo.ExamVO;
import com.zxj.friend.service.exam.IExamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/exam")
@Tag(name = "竞赛管理相关接口", description = "竞赛管理相关接口")
@RequiredArgsConstructor
@Slf4j
public class ExamController extends BaseController {
    private final IExamService examService;

    @GetMapping("/semiLogin/list")
    @Operation(summary = "数据库版:获取竞赛列表", description = "数据库版:分页查询竞赛列表")
    public R<PageVO<ExamVO>> pageQuery(ExamQueryPageDTO examQueryPageDTO) {
        log.info("接收到分页获取竞赛信息请求, 请求参数：{}", examQueryPageDTO);
        return R.ok(examService.pageQuery(examQueryPageDTO));
    }

    @GetMapping("/semiLogin/redis/list")
    @Operation(summary = "redis版: 获取竞赛列表", description = "redis版: 分页查询竞赛列表")
    public R<PageVO<ExamVO>> redisList(ExamQueryPageDTO examQueryPageDTO) {
        log.info("接收到Redis版本分页获取竞赛信息请求, 请求参数：{}", examQueryPageDTO);
        return R.ok(examService.redisList(examQueryPageDTO));
    }


    @GetMapping("/getFirstQuestion")
    @Operation(summary = "获取竞赛第一题", description = "获取竞赛中的第一题")
    @Parameter(name = "examId", description = "竞赛ID", required = true)
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    public R<String> getFirstQuestion(Long examId) {
        // 代码逻辑： 获取竞赛中题目的顺序列表   先从redis  redis中没有数据查询数据库  list  数据类型  key: e:q:l:examId   value : questionId
        // 排在第一个的题目  返回给前端
        log.info("接收到获取竞赛第一题请求, 请求参数：{}", examId);
        return R.ok(examService.getFirstQuestion(examId));
    }

    @GetMapping("/preQuestion")
    @Operation(summary = "获取竞赛上一题", description = "获取竞赛上一题")
    @Parameter(name = "examId", description = "竞赛ID", required = true)
    @Parameter(name = "questionId", description = "题目ID", required = true)
    public R<String> preQuestion(Long examId, Long questionId) {
        log.info("接收到获取竞赛上一题请求, 请求参数：examId: {}, questionId: {}", examId, questionId);
        return R.ok(examService.preQuestion(examId, questionId));
    }

    @GetMapping("/nextQuestion")
    @Operation(summary = "获取竞赛下一题", description = "获取竞赛下一题")
    @Parameter(name = "examId", description = "竞赛ID", required = true)
    @Parameter(name = "questionId", description = "题目ID", required = true)
    public R<String> nextQuestion(Long examId, Long questionId) {
        log.info("接收到获取竞赛下一题请求, 请求参数：examId: {}, questionId: {}", examId, questionId);
        return R.ok(examService.nextQuestion(examId, questionId));
    }

    @GetMapping("/rank/list")
    @Operation(summary = "获取竞赛排行榜", description = "获取竞赛排行榜")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    public R<PageVO<ExamRankVO>> rankList(ExamRankDTO examRankDTO) {
        log.info("接收到获取竞赛排行榜请求, 请求参数：{}", examRankDTO);
        return R.ok(examService.rankList(examRankDTO));
    }
}
